package com.nku.mason.masonframework.aop;

import java.lang.reflect.Method;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
 * 切面代理模板
 * 
 * 代理类需要扩展AspectProxy抽象类，还需要带有Aspect注解，只有满足这两个条件，才能根据Aspect注解中所定义的注解属性去获取该注解所对应的目标类集合
 * 
 * @author Mason
 * @date 2017年3月20日
 */
public abstract class AspectProxy implements Proxy{
	
	private static final Logger LOGGER = LoggerFactory.getLogger(AspectProxy.class);
	
	@Override
	public Object doProxy(ProxyChain proxyChain) throws Throwable {
		Object result = null;
		
		Class<?> cls = proxyChain.getTargetClass();
		Method method = proxyChain.getTargetMethod();
		Object[] params = proxyChain.getMethodParams();
		
		//代理调用逻辑部分
		begin();
		try {
			if (intercept(cls, method, params)) {
				before(cls, method, params);
				result = proxyChain.doProxyChain();
				after(cls, method, params);
			} else {
				result = proxyChain.doProxyChain();
			}
		} catch (Exception e) {
			LOGGER.error("proxy failure",e);
			error(cls, method, params, e);
			throw e;
		}
		end();
		/**************************************************/
		
		return result;
	}
	

	//可以被重写的模板方法
	/******************************************************************************************/
	public void begin(){
		//TODO
	}
	
	public void end(){
		//TODO
	}
	
	public void before(Class<?> cls, Method method, Object[] params) throws Throwable{
		//TODO
	}
	
	public void after(Class<?> cls, Method method, Object[] params) throws Throwable{
		//TODO
	}
	
	public void error(Class<?> cls, Method method, Object[] params, Throwable e){
		//TODO
	}
	
	public boolean intercept(Class<?> cls, Method method, Object[] params) throws Throwable{
		return true;
	}
	/******************************************************************************************/
}























































